//
//  GTAcounts.swift
//  swiftWEIbo
//
//  Created by mac－gt on 15/11/24.
//  Copyright © 2015年 mac－gt. All rights reserved.
//

import UIKit

class GTAcounts: NSObject,NSCoding {
    var access_token: String?
    var expiresDate: NSDate?
    var name: String?
    var avatar_large:String?
    var expires_in: NSTimeInterval=0{
        didSet{
            self.expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var uid: String?
     static let savePath=NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory,NSSearchPathDomainMask.UserDomainMask, true).last!+"/account.plist"
     init(dict: [String:AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    private static var userInfo: GTAcounts?
    override var description: String {
        let properties = ["access_token", "expires_in", "uid","expiresDate"]
        return "\(dictionaryWithValuesForKeys(properties))"
    }
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expiresDate, forKey: "expiresDate")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(name, forKey: "name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        
    }
    required init?(coder aDecoder: NSCoder) {
     access_token=aDecoder.decodeObjectForKey("access_token") as? String
       expires_in=aDecoder.decodeDoubleForKey("expires_in")
       expiresDate=aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        uid=aDecoder.decodeObjectForKey("uid") as? String
        name=aDecoder.decodeObjectForKey("name") as? String
        avatar_large=aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    func saveAccount(){
        NSKeyedArchiver .archiveRootObject(self, toFile:GTAcounts.savePath)
      
    }
    class func loadAccount()->GTAcounts?{
        if userInfo == nil {
          
            userInfo = NSKeyedUnarchiver.unarchiveObjectWithFile(savePath) as? GTAcounts
        }

        if userInfo != nil && userInfo!.expiresDate?.compare(NSDate()) ==  NSComparisonResult.OrderedDescending {
           
            return userInfo
        }
        
        return nil
    }
    class func isLogin()->Bool{
        if GTAcounts.loadAccount() != nil{
            return true
        }
        return false
    }
    func loadUserInfo(finish: (error: NSError?) -> ()){
        GTNetworking.sharedInsatance.loadUserInfo { (result, error) -> () in
            if error != nil || result==nil{
                finish(error: error)
                return
            }
            self.name=result!["name"] as? String
            self.avatar_large=result!["avatar_large"] as? String
            self.saveAccount()
            GTAcounts.userInfo=self
            finish(error: nil)
        }
      
    }
}